﻿namespace IBTrader.Indicator.Oscillator
{
    using IBTrader.Modules.Read;
    using IBTrader.Charts;
    using System;

    /// <summary>
    /// Standard deviation is a statistical term that measures the amount of variability or dispersion around an average. 
    /// Standard deviation is also a measure of volatility. 
    /// Generally speaking, dispersion is the difference between the actual value and the average value. 
    /// The larger this dispersion or variability is, the higher the standard deviation. 
    /// The smaller this dispersion or variability is, the lower the standard deviation. 
    /// Chartists can use the Standard Deviation to measure expected risk and determine the significance of certain price movements.
    /// </summary>
    class Volatility : BasePrices
    {
        private readonly Prices Deviations;
        public Volatility(Worker worker, int interval = 10) : base(worker, new ChartArgs { Group = 1 }, interval) { Deviations = new Prices(interval); }
        public override void Add(object _, Line line)
        {
            Prices.Add(line.Price);
            Deviations.Add(Math.Pow(line.Price - Prices.Average(), 2));
            OnHit(line, Math.Sqrt(Deviations.Average()));
        }
    }
}
